﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MyOA.DAL;
using MyOA.Model;


namespace MyOA.DAL
{
    public class ManualSignSearchService
    {
        DBHelper db = new DBHelper();
        UserInfoService userInfoService = new UserInfoService();
        /// <summary>
        /// 查询员工信息
        /// </summary>
        /// <returns></returns>
        public IList<ManualSign> GetManualSignsBySelectedConditions(DateTime startTime, DateTime endTime, string loginId, string userName, string departName, string branchName)
        {
            string sqlDepartName = "";
            if (departName != "" && departName != "==请选择==")
            {
                sqlDepartName = " and DepartName='" + departName + "' ";
            }


            string sqlBranchName = "";
            if (branchName != "" && branchName != "==请选择==" && branchName != "-1")
            {
                sqlBranchName = " and BranchName='" + branchName + " '";
            }

            string sqlUserId = "";
            if (loginId != "" && loginId != "请输入员工号......")
            {
                sqlUserId = " and loginId='" + loginId + "' ";
            }

            string sqlUserName = "";
            if (userName != "")
            {
                sqlUserName = " and UserName like '%" + userName + "%'";
            }
            string sql = "SELECT * FROM viewManualSignHistory where  1=1";

            sql += sqlDepartName;
            sql += sqlBranchName;
            sql += sqlUserId;
            sql += sqlUserName;
            if (startTime.ToString() != "0001-1-1 0:00:00" && endTime.ToString() != "0001-1-1 0:00:00")
            {
                string s=string.Format(" and signtime between '{0}' and '{1}'",startTime,endTime);
                sql += s;
            }
            db.CloseConnection();
            return GetManualSignsBySql(sql);

        }


        /// <summary>
        /// 根据SQL语名查询员工信息
        /// </summary>
        /// <param name="safeSql"></param>
        /// <returns></returns>
        private IList<ManualSign> GetManualSignsBySql(string safeSql)
        {
            List<ManualSign> list = new List<ManualSign>();
            ManualSign manualSign = null;
            DataTable table = db.getDataTable_1(safeSql);

            foreach (DataRow row in table.Rows)
            {
                manualSign = new ManualSign();

                manualSign.SignId = (int)row["SignId"];
                manualSign.SignTime = (DateTime)row["SignTime"];
                manualSign.SignDesc = (string)row["SignDesc"];
                manualSign.SignTag = (int)row["SignTag"];
                manualSign.User.Depart.DepartName = (string)row[8];
                manualSign.User.Depart.Branch.BranchName = (string)row[9];
                manualSign.User.UserName = (string)row[5];

                list.Add(manualSign);
            }
            db.CloseConnection();
            return list;

        }
    }
}


